//
//  AppDelegate.m
//  EquesCat
//
//  Created by Chun Hung Cheung on 2024/12/17.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "BaseTabBarController.h"
#import <EquesBusiness/YKIPinfo.h>
#import <EquesBusiness/YKBusinessFramework.h>
#import "LoginViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self get_server];
    
    // 开始监听网络连接状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:AFNetworkingReachabilityDidChangeNotification  object: nil];
    
    YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];

    if (info.auto_login) {
        
         self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
         self.window.backgroundColor = [UIColor whiteColor];
         self.window.rootViewController = [[BaseTabBarController alloc]init];
        [self.window makeKeyAndVisible];
        
       

        [YKBusinessFramework loginWithUserName:info.user token:[Tooles getuserToken] appKey:ThirdPartyDemoAppKey server_adds:SERVER_ADDRS];
    }else{
    
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        self.window.rootViewController = [[LoginViewController alloc]init];
       [self.window makeKeyAndVisible];
        
    }
    
    
    return YES;
}

- (void)reachabilityChanged: (NSNotification* )note
{
    
    NSLog(@"监听网络环境 -->%@ 网络状态--->%ld",[note object],(long)[AFNetworkReachabilityManager sharedManager].networkReachabilityStatus);
    
    if ([AFNetworkReachabilityManager sharedManager].isReachable){
       
        YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
        if (!info) {
            [self get_server];
        }
        
    }
    
}


 - (void)applicationWillEnterForeground:(UIApplication *)application
{
   
    //连接设备热点后 自动跳扫描Wi-Fi界面通知
    dispatch_time_t delayTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5/*延迟执行时间*/ * NSEC_PER_SEC));
    dispatch_after(delayTime1, dispatch_get_main_queue(), ^{
         [[NSNotificationCenter defaultCenter] postNotificationName:@"ScanNavBarChange" object:nil];
    });
    
    NSLog(@"applicationWillEnterForeground 进入前台 socket登录");
    if (![YKBusinessFramework equesIsLogin]) {
        
        YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
        if (info.auto_login) {
    
            [YKBusinessFramework loginWithUserName:info.user token:[Tooles getuserToken] appKey:ThirdPartyDemoAppKey server_adds:SERVER_ADDRS];
        }
    
    }
    
    
    
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"applicationDidEnterBackground 退到后台 sockeet退出");
    if ([YKBusinessFramework equesIsLogin]) {
        [YKBusinessFramework equesLogout];
    }
}


/*
  首次启动或注册前 需要获取服务器的分发地址
 */
- (void)get_server
{
//    YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
//    NSString *loginAddress = info.equesLoginIpAddress; //  IP
//    if (!loginAddress) {
//        NSString *appkey = @"cXN77a6QgTZ8vMZvz34zdGi9kJGupLhg";
//        NSString * appid = @"118";
//      [YKBusinessFramework equesInit:@"distribute.ecamzone.cc" withIsSecond:@"yes" withUserName:@"" andAppid:appid andAppkey:appkey update_ip:nil];
//    }
    
    
    YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
    
    [YKBusinessFramework get_server_ip:SERVER_ADDRS username:info.user];
    
    
}

- (void)login_sucess
{
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[BaseTabBarController alloc]init];
   [self.window makeKeyAndVisible];
}

- (void)login_out
{
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[LoginViewController alloc]init];
   [self.window makeKeyAndVisible];
}

@end
